package com.github.starry.fw.social.web.spider;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.github.starry.fw.social.web.spider.wechat.tasklet.CreateJavaTasklet;
import com.github.starry.fw.social.web.spider.wechat.tasklet.GetApiTasklet;
import com.github.starry.fw.social.web.spider.wechat.tasklet.GetWikiTasklet;

@SpringBootApplication
@EnableBatchProcessing
public class SampleBatchApplication {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Autowired
    private GetWikiTasklet getWikiTasklet;

    @Autowired
    private GetApiTasklet getApiTasklet;

    @Autowired
    private CreateJavaTasklet createJavaTasklet;

    @Bean
    public Job job() throws Exception {
        return this.jobs.get("job").start(step1()).build();
    }

    @Bean
    protected Step step1() throws Exception {
        return this.steps.get("step1").tasklet(getApiTasklet).build();
    }

    public static void main(String[] args) throws Exception {
        // System.exit is common for Batch applications since the exit code can be used to
        // drive a workflow
        System.exit(SpringApplication
                .exit(SpringApplication.run(SampleBatchApplication.class, args)));
    }

}